{{Extend "layout"}}
{{Block "title"}}{{if HasSuffix URL.Path "_add"}}{{"添加网站"|T}}{{else}}{{"修改网站"|T}}{{end}}{{/Block}}
{{Block "breadcrumb"}}
{{Super}}
<li class="active">{{if HasSuffix URL.Path "_add"}}{{"添加网站"|T}}{{else}}{{"修改网站"|T}}{{end}}</li>
{{/Block}}
{{Block "main"}}
<div class="row">
    <div class="col-md-12">
        <div class="block-flat">
          <div class="header">							
            <h3>{{if HasSuffix URL.Path "_add"}}{{"添加网站"|T}}{{else}}{{"修改网站"|T}}{{end}}</h3>
          </div>
          <div class="content">
              <form class="form-horizontal group-border-dashed" method="POST" action="">
              <div class="form-group">
                  <label class="col-sm-2 control-label">{{"分组"|T}}</label>
                  <div class="col-sm-8">
                      {{$v := Form "groupId"}}
                      <select class="form-control" name="groupId">
                          <option value="">{{"<无>"|T}}</option>
                          {{range $k,$g:=Stored.Get "groupList"}}
                          <option value="{{$g.Id}}" {{if eq $v (Str $g.Id)}} selected{{end}}>{{$g.Name}}</option>
                          {{end}}
                      </select>
                  </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"监听地址"|T}}</label>
                <div class="col-sm-8">
                    <input type="text" id="bind-address" name="domain" class="form-control" required="required" value="{{Form "domain"}}">
                    <div class="help-block">
                      {{"可以是网址、域名或IP。如果不提供端口则默认为2005，如果不提供协议(比如https://)则默认为http，如果不提供IP和域名则默认为0.0.0.0，如果只想允许本机访问请设置为localhost或127.0.0.1。"|T}}<br />
                      {{"域名支持通配符“*”(例如*.admpub.com)和环境变量(环境变量用花括号括起来，例如localhost:{$PORT})。"|T}}<br />
                      {{"例子"|T}}: 
                      <span class="badge badge-success clickable" onclick="App.insertAtCursor($('#bind-address')[0],$(this).text(),8,18);">https://admpub.com:443</span>
                      <span class="badge badge-primary clickable" onclick="App.insertAtCursor($('#bind-address')[0],$(this).text(),0,10);">admpub.com:80</span>
                      <span class="badge badge-danger clickable" onclick="App.insertAtCursor($('#bind-address')[0],$(this).text(),1,6);">:8080</span>
                      <span class="badge badge-info clickable" onclick="App.insertAtCursor($('#bind-address')[0],$(this).text(),10,14);">127.0.0.1:9999</span><br />
                      {{"监听地址如有多个，用空格隔开。"|T}}
                    </div>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"网站位置"|T}}</label>
                <div class="col-sm-8">
                    <input type="text" id="www-root-path" name="root" class="form-control typeahead" required="required" value="{{Form "root"}}" data-provide="typeahead">
                    <div class="help-block">{{"网站文件夹在服务器上的绝对路径"|T}}</div>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"网站名称"|T}}</label>
                <div class="col-sm-8">
                    <input type="text" name="name" class="form-control" value="{{Form "name"}}">
                    <div class="help-block">
                      {{"为了提高辨识度，您可以给网站起一个名字。"|T}}
                    </div>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"访问日志"|T}}</label>
                <div class="col-sm-8">
                  {{$v := Form "log" "1"}}
                  <div class="radio radio-primary radio-inline"> 
                    <input type="radio" value="1" id="log-enable" name="log" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}><label for="log-enable">{{"启用"|T}}</label> 
                  </div>
                  <div class="radio radio-danger radio-inline"> 
                    <input type="radio" value="0" id="log-disable" name="log" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}><label for="log-disable">{{"禁用"|T}}</label> 
                  </div>
                  <div class="fieldset bg-fc hide" id="log-settings">
                    {{if eq $v "1"}}{{Include "caddy/addon/form/log"}}{{end}}
                  </div>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"默认首页"|T}}</label>
                <div class="col-sm-8">
                    <input type="text" name="index" class="form-control" value="{{Form "index"}}">
                    <div class="help-block">{{"默认首页文件，多个之间用半角空格隔开，如不填写，默认为："|T}}<br/> 
                      index.html index.htm index.txt default.html default.htm default.txt
                    </div>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"响应Header"|T}}<a href="https://caddyserver.com/docs/header" target="_blank" data-toggle="tooltip" title="{{"查看文档"|T}}" class="normal">(?)</a></label>
                <div class="col-sm-8">
                  <div class="fieldset bg-fc">
                    <div class="form-group">
                          <label class="col-sm-2 control-label">{{"指定路径"|T}}</label>
                          <div class="col-sm-9">
                             <input type="text" class="form-control" name="header_path" value="{{Form "header_path"}}">
                             <div class="help-block">{{"可以针对某个起始路径设置header"|T}}</div>
                          </div>
                    </div>

                    <div class="form-group">
                          <label class="col-sm-2 control-label">{{"设置Header"|T}}</label>
                          <div class="col-sm-9">
                    <table>
								        <thead>
								        	<tr>
								        		<th style="width:50%;">{{"字段名"|T}}</th>
								        		<th>{{"值"|T}}</th>
								        		<th width="70">{{"删除"|T}}</th>
								        	</tr>
								        </thead>
								        <tbody>
                                {{$vals := FormValues "header_v"}}
                                {{$vlen := len $vals}}
                                {{range $k,$v:=FormValues "header_k"}}
                                    {{if lt $k $vlen}}
<tr>
	<td><input type="text" class="form-control" name="header_k" value="{{$v}}"></td>
	<td><input type="text" class="form-control" name="header_v" value="{{index $vals $k}}"></td>
	<td class="text-center">
    <button type="button" class="btn btn-danger" onclick="$(this).parent().parent().remove()">
      <i class="fa fa-times"></i>
    </button>
  </td>
</tr>
                                        
                                    {{end}}
                                {{end}}
								        	<tr>
								        		<td colspan="3" class="text-center primary-emphasis" style="cursor:pointer" id="headerAddVarBtn" onclick="addKVs(this,'header_k','header_v');"><i class="fa fa-plus"></i></td>
								        	</tr>
								        </tbody>
							      </table>
                    <div class="help-block">{{"如果要将已存在的header删掉，请在名称前使用减号(-)并将其值留空"|T}}</div>
                  </div>
                  
                          </div>
                    </div>

                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"HTTPS"|T}}<a href="https://caddyserver.com/docs/tls" target="_blank" data-toggle="tooltip" title="{{"查看文档"|T}}" class="normal">(?)</a></label>
                <div class="col-sm-8">
                    {{$v := Form "tls"}}
                    <div class="radio radio-primary radio-inline"> 
                      <input type="radio" value="1" id="tls-enable" name="tls" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}><label for="tls-enable">{{"启用"|T}}</label> 
                    </div>
                    <div class="radio radio-danger radio-inline"> 
                      <input type="radio" value="0" id="tls-disable" name="tls" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}><label for="tls-disable">{{"禁用"|T}}</label> 
                    </div>
                    <div class="fieldset bg-fc hide" id="tls-settings">
                        {{if eq $v "1"}}{{Include "caddy/addon/form/tls"}}{{end}}
                    </div>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"GZIP"|T}}<a href="https://caddyserver.com/docs/gzip" target="_blank" data-toggle="tooltip" title="{{"查看文档"|T}}" class="normal">(?)</a></label>
                <div class="col-sm-8">
                  {{$v := Form "gzip"}}
                  <div class="radio radio-primary radio-inline"> 
                    <input type="radio" value="1" id="gzip-enable" name="gzip" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}><label for="gzip-enable">{{"启用"|T}}</label> 
                  </div>
                  <div class="radio radio-danger radio-inline"> 
                    <input type="radio" value="0" id="gzip-disable" name="gzip" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}><label for="gzip-disable">{{"禁用"|T}}</label> 
                  </div>
                    <div class="fieldset bg-fc hide" id="gzip-settings">
                        {{if eq $v "1"}}{{Include "caddy/addon/form/gzip"}}{{end}}
                    </div>
                </div>
              </div>
              
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"FastCGI"|T}}<a href="https://caddyserver.com/docs/fastcgi" target="_blank" data-toggle="tooltip" title="{{"查看文档"|T}}" class="normal">(?)</a></label>
                <div class="col-sm-8">
                  {{$v := Form "fastcgi"}}
                  <div class="radio radio-primary radio-inline"> 
                    <input type="radio" value="1" id="fastcgi-enable" name="fastcgi" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}><label for="fastcgi-enable">{{"启用"|T}}</label> 
                  </div>
                  <div class="radio radio-danger radio-inline"> 
                    <input type="radio" value="0" id="fastcgi-disable" name="fastcgi" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}><label for="fastcgi-disable">{{"禁用"|T}}</label> 
                  </div>
                  <div class="fieldset bg-fc hide" id="fastcgi-settings">
                    {{if eq $v "1"}}{{Include "caddy/addon/form/fastcgi"}}{{end}}
                  </div>
                </div>
              </div>
              
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"Proxy"|T}}<a href="https://caddyserver.com/docs/proxy" target="_blank" data-toggle="tooltip" title="{{"查看文档"|T}}" class="normal">(?)</a></label>
                <div class="col-sm-8">
                  {{$v := Form "proxy"}}
                  <div class="radio radio-primary radio-inline"> 
                    <input type="radio" value="1" id="proxy-enable" name="proxy" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}><label for="proxy-enable">{{"启用"|T}}</label> 
                  </div>
                  <div class="radio radio-danger radio-inline"> 
                    <input type="radio" value="0" id="proxy-disable" name="proxy" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}><label for="proxy-disable">{{"禁用"|T}}</label> 
                  </div>
                  <div class="fieldset bg-fc hide" id="proxy-settings">
                    {{if eq $v "1"}}{{Include "caddy/addon/form/proxy"}}{{end}}
                  </div>
                </div>
              </div>

              
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"文件服务"|T}}<a href="https://caddyserver.com/docs/browse" target="_blank" data-toggle="tooltip" title="{{"查看文档"|T}}" class="normal">(?)</a></label>
                <div class="col-sm-8">
                  {{$v := Form "browse"}}
                  <div class="radio radio-primary radio-inline"> 
                    <input type="radio" value="1" id="browse-enable" name="browse" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}><label for="browse-enable">{{"启用"|T}}</label> 
                  </div>
                  <div class="radio radio-danger radio-inline"> 
                    <input type="radio" value="0" id="browse-disable" name="browse" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}><label for="browse-disable">{{"禁用"|T}}</label> 
                  </div>
                  <div class="fieldset bg-fc hide" id="browse-settings">
                    {{if eq $v "1"}}{{Include "caddy/addon/form/browse"}}{{end}}
                  </div>
                </div>
              </div>

              <!-- <div class="form-group">
                <label class="col-sm-2 control-label">{{"静态文件缓存"|T}}<a href="https://caddyserver.com/docs/http.expires" target="_blank" data-toggle="tooltip" title="{{"查看文档"|T}}" class="normal">(?)</a></label>
                <div class="col-sm-8">
                  {{$v := Form "expires"}}
                  <div class="radio radio-primary radio-inline"> 
                    <input type="radio" value="1" id="expires-enable" name="expires" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}><label for="expires-enable">{{"启用"|T}}</label> 
                  </div>
                  <div class="radio radio-danger radio-inline"> 
                    <input type="radio" value="0" id="expires-disable" name="expires" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}><label for="expires-disable">{{"禁用"|T}}</label> 
                  </div>
                  <div class="fieldset bg-fc hide" id="expires-settings">
                    {{if eq $v "1"}}{{Include "caddy/addon/form/expires"}}{{end}}
                  </div>
                </div>
              </div> -->
              
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"IP过滤"|T}}<a href="https://caddyserver.com/docs/http.ipfilter" target="_blank" data-toggle="tooltip" title="{{"查看文档"|T}}" class="normal">(?)</a></label>
                <div class="col-sm-8">
                  {{$v := Form "ipfilter"}}
                  <div class="radio radio-primary radio-inline"> 
                    <input type="radio" value="1" id="ipfilter-enable" name="ipfilter" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}><label for="ipfilter-enable">{{"启用"|T}}</label> 
                  </div>
                  <div class="radio radio-danger radio-inline"> 
                    <input type="radio" value="0" id="ipfilter-disable" name="ipfilter" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}><label for="ipfilter-disable">{{"禁用"|T}}</label> 
                  </div>
                  <div class="fieldset bg-fc hide" id="ipfilter-settings">
                    {{if eq $v "1"}}{{Include "caddy/addon/form/ipfilter"}}{{end}}
                  </div>
                </div>
              </div>

<div class="form-group">
  <label class="col-sm-2 control-label">{{"网址重写"|T}}<a href="https://caddyserver.com/docs/rewrite" target="_blank" data-toggle="tooltip" title="{{"查看文档"|T}}" class="normal">(?)</a></label>
  <div class="col-sm-8">
     <textarea type="text" id="rewrite_rule" class="form-control" style="min-height:200px" name="rewrite_rule">{{Form "rewrite_rule"}}</textarea>
     <div class="help-block">
        {{"网址重写规则支持"|T}}<a href='javascript:;' onclick="showRequestPlaceholders('#rewrite_rule')" style="font-weight:bold">{{"占位符"|T}}</a><br/>
         1. {{"简洁语法："|T}}<a href="javascript:;" onclick="App.insertAtCursor($('#rewrite_rule')[0],'rewrite from to\n',8,12);"><strong>rewrite</strong> <em data-toggle="tooltip" title="not: {{"不匹配。可选"|T}}">[not]</em> <em style="color:green" data-toggle="tooltip" title="from: {{"匹配路径。支持正则"|T}}">from</em> <em style="color:green" data-toggle="tooltip" title="to: {{"目标路径。多个目标路径用半角空格隔开，to的作用与“高级语法”中的相同"|T}}">to</em></a>
         {{"例如："|T}}<a href="javascript:;" onclick="App.insertAtCursor($('#rewrite_rule')[0],'rewrite / {path} /index.php{uri}');">[{{"插入"|T}}]</a><br />
         <pre>rewrite / {path} /index.php{uri}</pre>
         2. {{"高级语法："|T}}<a href="javascript:;" onclick="insertAdvRewrite()">[{{"插入"|T}}]</a><br />
<style>
.adv-rewrite-rule [data-popover]{border-bottom:1px dashed #aaa;cursor:help;}
</style>
<pre class="adv-rewrite-rule">
<strong>rewrite</strong> <em style="color:green" data-html="true" data-popover="popover" data-content="{{"正则(网址重写之前匹配的路径)<br />默认为“/”"|T}}" title="basepath">[basepath]</em> {
    <strong>regexp</strong> <span data-html="true" data-popover="popover" data-content="{{"匹配规则。<br />支持正则表达式。<br />对于高负载的网站应该尽量避免使用正则表达式"|T}}" title="pattern">pattern</span>
    <strong>ext</strong>    <span data-html="true" data-popover="popover" data-content="{{"带点号的扩展名(多个用空格隔开)<br />带前缀“!”则表示排除，如果设为“/”则表示没有扩展名"|T}}" title="extensions...">extensions...</span>
    <strong>if</strong>     <span data-html="true" data-popover="popover" data-content="if用来指定重写条件，支持多个if。<br />
  <strong>a</strong>和<strong>b</strong>可以是字符串或<a href='javascript:;' onclick='showRequestPlaceholders()'>占位符</a>；<br />
  <strong>cond</strong>是条件，支持以下条件：<p>
  <a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>is</strong></a> - 等于<br />
  <a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>not</strong></a> - 不等于<br />
  <a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>has</strong></a> - 包含子字符串<br />
  <a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>not_has</strong></a> - 不包含子字符串<br />
  <a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>starts_with</strong></a> - 带前缀<br />
  <a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>not_starts_with</strong></a> - 不带前缀<br />
  <a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>ends_with</strong></a> - 带后缀<br />
  <a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>not_ends_with</strong></a> - 不带后缀<br />
  <a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>match</strong></a> - 匹配正则表达式<br />
  <a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>not_match</strong></a> - 不匹配正则表达式<br /></p>" title="a cond b">a cond b</span>
    <strong>if_op</strong>  <em style="color:green" data-html="true" data-popover="popover" data-content="{{"if条件之间的逻辑关系(默认是and)"|T}}" title="and|or">[and|or]</em>
    <strong>to</strong>     <span data-html="true" data-popover="popover" data-content="{{"网址替换规则。<br />可以是一个或多个以空格隔开的路径；<br />支持<a href='javascript:;' onclick='showRequestPlaceholders()'>占位符</a>；<br />支持正则捕获编号(比如：{1},{2}等)<br /><br />服务器将依次检查目标路径的文件或文件夹(如果以/结尾则认为是文件夹)是否存在，一旦找到存在的目标则向其转向，如果所有的目标路径都不存在，则默认使用最后一个；"|T}}" title="destinations...">destinations...</span>
}
</pre>
     </div>
  </div>
</div>

              <div class="form-group">
                <label class="col-sm-2 control-label">{{"备注"|T}}</label>
                <div class="col-sm-8">
                  <textarea class="form-control" name="remark">{{Form "remark"}}</textarea>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"网站状态"|T}}</label>
                <div class="col-sm-8">
                  {{$v := Form "disabled"}}
                  <div class="radio radio-primary radio-inline"> 
                    <input type="radio" value="N" id="disabled-N" name="disabled"{{if or (eq $v "N") (eq $v "")}} checked{{end}}><label for="disabled-N">{{"启用"|T}}</label> 
                  </div>
                  <div class="radio radio-danger radio-inline"> 
                    <input type="radio" value="Y" id="disabled-Y" name="disabled"{{if eq $v "Y"}} checked{{end}}><label for="disabled-Y">{{"禁用"|T}}</label> 
                  </div>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"是否重启"|T}}</label>
                <div class="col-sm-8">
                  <div class="checkbox checkbox-primary radio-inline"> 
                    <input type="checkbox" value="1" id="restart-enable" name="restart"><label for="restart-enable">{{"保存并重启服务器"|T}}</label> 
                  </div>
                </div>
              </div>

              <div class="form-group form-submit-group">
									<div class="col-sm-9 col-sm-offset-2">
									  <button type="submit" class="btn btn-primary btn-lg"><i class="fa fa-save"></i> {{"保存"|T}}</button>
									  <button type="reset" class="btn btn-default btn-lg"><i class="fa fa-refresh"></i> {{"重置"|T}}</button>
									</div>
							</div>
            </form>
          </div><!-- /.content -->
        </div><!-- /.block-flat -->
    </div>
</div>
{{/Block}}
{{Block "footer"}}
<script type="text/javascript" src="__ASSETS__/js/behaviour/textarea-editor.js"></script>
<script type="text/template" id="tmplAddVariableRow">
<tr>
	<td><input type="text" class="form-control" name="{k}" value=""></td>
	<td><input type="text" class="form-control" name="{v}" value=""></td>
	<td class="text-center">
    <button type="button" class="btn btn-danger" onclick="$(this).parent().parent().remove()">
      <i class="fa fa-times"></i>
    </button>
  </td>
</tr>
</script>
{{Modal "__TMPL__/caddy/request_placeholders.yaml"}}
<script>
var inputRequestPlaceholder=null;
function showRequestPlaceholders(a){
  if(a==null) a='#rewrite_rule';
  inputRequestPlaceholder=a;
  $('#request-placeholders-modal').niftyModal('show');
}
function insertAdvRewrite(){
  App.insertAtCursor($('#rewrite_rule')[0],'rewrite / {\n\
    regexp pattern\n\
    ext    extensions...\n\
    if     a cond b\n\
    if_op  [and|or]\n\
    to     destinations...\n\
}\n',23,31);
}
function insertCondForRewrite(obj){
  App.insertAtCursor($('#rewrite_rule')[0],$(obj).children('strong').text()+' ');
}
function addKVs(obj,k,v){
  var tmpl=$('#tmplAddVariableRow').html();
  tmpl=tmpl.replace(/\{k\}/g,k);
  tmpl=tmpl.replace(/\{v\}/g,v);
  $(obj).parent().before(tmpl);
}
function initReqPlaceholdersModal(){
  $('#request-placeholders-modal').find('.modal-body').css({"max-height":$(window).height()-200});
}
$(function(){
initReqPlaceholdersModal();
$(window).on('resize',function(){
  initReqPlaceholdersModal();
});
$('[data-slide-settings]').each(function(){
  var eid='#'+$(this).attr('name')+'-settings';
  if($(eid).data('history'))$(eid).data('history',null);
});
$('[data-slide-settings]').on('click',function(){
  var slide=$(this).data('slide-settings');
  if(slide=="")slide=$(this).val()=='1'?'show':'hide';
  var eid='#'+$(this).attr('name')+'-settings';
  var history=$.trim($(eid).html());
  if(slide=='show'){
    if(!history){
      if($(eid).data('history')){
        $(eid).html($(eid).data('history'));
      }else{
        $.get(BACKEND_URL+'/caddy/addon_form',{addon:$(this).attr('name')},function(r){
          $(eid).html(r);
        },'html');
      }
    }
    $(eid).removeClass('hide');
  }else{
    if(history)$(eid).data('history',history);
    $(eid).empty().addClass('hide');
  }
});
$('[data-slide-settings="show"]:checked').trigger('click');
$('#request-placeholders-modal .modal-body table tbody td b').off().on('click',function(){
  App.insertAtCursor($(inputRequestPlaceholder)[0],$(this).text());
  $('#request-placeholders-modal').niftyModal('hide');
});
$('[data-popover="popover"]').on('click',function(){
  $(this).siblings('[data-popover="popover"]').popover('hide');
});
App.searchFS('#www-root-path',20,'dir');
});
</script>
{{/Block}}